package org.mybatis.generator.api.plus;

import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.io.*;

/**
 * @author wangfupeng
 */
public class MybatisUtilFeignWrapper {

    public static void writeFeignWrapper(String basePackageName, String aspectPackageName, String targetProject, TopLevelClass topLevelClass, IntrospectedTable introspectedTable, String tableName, String servicePackage) {
        File file = null;
        OutputStream fileOutputStream = null;
        BufferedWriter bufferedWriter = null;
        try {
            String packageNameFolder = basePackageName.replaceAll("\\.", "/");
            targetProject = targetProject.replaceAll("\\./", "");
            File folder = new File(targetProject + "/" + packageNameFolder + "/feign/common/wrapper");
            folder.mkdirs();
            file = new File(targetProject + "/" + packageNameFolder + "/feign/common/wrapper/" + tableName + "ClientWrapper.java");
            //如果文件不存在，则创建文件，如果已存在，则覆盖
            file.createNewFile();
            fileOutputStream = new FileOutputStream(file);
            bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream, "utf-8"));
            bufferedWriter.write("package " + basePackageName + ".feign.common.wrapper;\n" +
                    "\n" +
                    "import com.gitee.fufu669.response.ListResponse;\n" +
                    "import com.gitee.fufu669.response.SimpleResponse;\n" +
                    "import " + basePackageName + ".bean.model." + tableName + ";\n" +
                    "import " + basePackageName + ".feign.common.client." + tableName + "Client;\n" +
                    "import " + basePackageName + ".config.exception.CurrentServerException;\n" +
                    "import org.springframework.stereotype.Component;\n" +
                    "import org.slf4j.Logger;\n" +
                    "import org.slf4j.LoggerFactory;\n" +
                    "import org.springframework.beans.factory.annotation.Autowired;\n" +
                    "import java.util.Date;\n" +
                    "\n" +
                    "/*" + introspectedTable.getRemarks() + "的feign远程调用的封装调用层*/\n" +
                    "@Component\n" +
                    "public class " + tableName + "ClientWrapper {\n" +
                    "\n" +
                    "    public static final Logger logger = LoggerFactory.getLogger(" + tableName + "ClientWrapper.class);\n" +
                    "    public static final int STATUS_SUCCESS = 200;\n" +
                    "\n" +
                    "    @Autowired\n" +
                    "    private " + tableName + "Client " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "Client;\n" +
                    "\n" +
                    "    /*创建" + introspectedTable.getRemarks() + "*/\n" +
                    "    public SimpleResponse create(String token, " + tableName + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "){\n" +
                    "        //拿到创建后的实例\n" +
                    "        SimpleResponse simpleResponse = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "Client.create(token, " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + ");\n" +
                    "        //如果创建成功\n" +
                    "        if(simpleResponse.getStatus()==STATUS_SUCCESS){\n" +
                    "            //返回结果\n" +
                    "            return simpleResponse;\n" +
                    "        }\n" +
                    "        //抛出异常\n" +
                    "        throw new CurrentServerException(simpleResponse.getStatus(),simpleResponse.getMessage());\n" +
                    "    }\n" +
                    "\n");
            String pathForPrimaryKeyColumns = MybatisUtilCommon.getPathForPrimaryKeyColumns(introspectedTable);
            if (!"".equals(pathForPrimaryKeyColumns)) {
                bufferedWriter.write(
                        "" +
                                "    /*删除" + introspectedTable.getRemarks() + "*/\n" +
                                "    public SimpleResponse delete(String token, " + MybatisUtilCommon.getParameterWithoutAnnotationForPrimaryKeyColumns(introspectedTable) + "){\n" +
                                "        //拿到删除后的实例\n" +
                                "        SimpleResponse simpleResponse = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "Client.delete(token, " + MybatisUtilCommon.getParameterWithoutAnnotationWithoutTypeForPrimaryKeyColumns(introspectedTable) + ");\n" +
                                "        //如果删除成功\n" +
                                "        if(simpleResponse.getStatus()==STATUS_SUCCESS){\n" +
                                "            //返回结果\n" +
                                "            return simpleResponse;\n" +
                                "        }\n" +
                                "        //抛出异常\n" +
                                "        throw new CurrentServerException(simpleResponse.getStatus(),simpleResponse.getMessage());\n" +
                                "    }\n" +
                                "\n");
                if (MybatisUtilCommon.getExistNonePrimaryKeyColumns(introspectedTable)) {
                    bufferedWriter.write("" +
                            "    /*更改" + introspectedTable.getRemarks() + "*/\n" +
                            "    public SimpleResponse update(String token, " + tableName + " " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "){\n" +
                            "        //拿到更改后的实例\n" +
                            "        SimpleResponse simpleResponse = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "Client.update(token, " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + ");\n" +
                            "        //如果更改成功\n" +
                            "        if(simpleResponse.getStatus()==STATUS_SUCCESS){\n" +
                            "            //返回结果\n" +
                            "            return simpleResponse;\n" +
                            "        }\n" +
                            "        //抛出异常\n" +
                            "        throw new CurrentServerException(simpleResponse.getStatus(),simpleResponse.getMessage());\n" +
                            "    }\n" +
                            "\n" +
                            "    /*获取远程自动缓存" + introspectedTable.getRemarks() + "实例*/\n" +
                            "    public SimpleResponse get(String token, " + MybatisUtilCommon.getParameterWithoutAnnotationForPrimaryKeyColumns(introspectedTable) + "){\n" +
                            "        //拿到获取的实例\n" +
                            "        SimpleResponse simpleResponse = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "Client.get(token, " + MybatisUtilCommon.getParameterWithoutAnnotationWithoutTypeForPrimaryKeyColumns(introspectedTable) + ");\n" +
                            "        //如果成功\n" +
                            "        if(simpleResponse.getStatus()==STATUS_SUCCESS){\n" +
                            "            //返回结果\n" +
                            "            return simpleResponse;\n" +
                            "        }\n" +
                            "        //抛出异常\n" +
                            "        throw new CurrentServerException(simpleResponse.getStatus(),simpleResponse.getMessage());\n" +
                            "    }\n" +
                            "\n" +
                            "    /*获取远程只读缓存" + introspectedTable.getRemarks() + "实例*/\n" +
                            "    public SimpleResponse mock(String token, " + MybatisUtilCommon.getParameterWithoutAnnotationForPrimaryKeyColumns(introspectedTable) + "){\n" +
                            "        //拿到获取的实例\n" +
                            "        SimpleResponse simpleResponse = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "Client.mock(token, " + MybatisUtilCommon.getParameterWithoutAnnotationWithoutTypeForPrimaryKeyColumns(introspectedTable) + ");\n" +
                            "        //如果成功\n" +
                            "        if(simpleResponse.getStatus()==STATUS_SUCCESS){\n" +
                            "            //返回结果\n" +
                            "            return simpleResponse;\n" +
                            "        }\n" +
                            "        //抛出异常\n" +
                            "        throw new CurrentServerException(simpleResponse.getStatus(),simpleResponse.getMessage());\n" +
                            "    }\n" +
                            "\n");
                }
            }
            bufferedWriter.write("" +
                    "    /*获取远程自动缓存" + introspectedTable.getRemarks() + "实例列表*/\n" +
                    "    public ListResponse list(String token, Integer page, Integer limit, String orderBy){\n" +
                    "        //拿到获取的实例\n" +
                    "        ListResponse listResponse = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "Client.list(token, page, limit, orderBy);\n" +
                    "        //如果成功\n" +
                    "        if(listResponse.getStatus()==STATUS_SUCCESS){\n" +
                    "            //返回结果\n" +
                    "            return listResponse;\n" +
                    "        }\n" +
                    "        //抛出异常\n" +
                    "        throw new CurrentServerException(listResponse.getStatus(),listResponse.getMessage());\n" +
                    "    }\n" +
                    "    \n" +
                    "\n" +
                    "    /*获取远程只读缓存无验证" + introspectedTable.getRemarks() + "实例列表*/\n" +
                    "    public ListResponse listMockWithoutAuthUser(Integer page, Integer limit, String orderBy){\n" +
                    "        //拿到获取的实例\n" +
                    "        ListResponse listResponse = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "Client.listMockWithoutAuthUser(page, limit, orderBy);\n" +
                    "        //如果成功\n" +
                    "        if(listResponse.getStatus()==STATUS_SUCCESS){\n" +
                    "            //返回结果\n" +
                    "            return listResponse;\n" +
                    "        }\n" +
                    "        //抛出异常\n" +
                    "        throw new CurrentServerException(listResponse.getStatus(),listResponse.getMessage());\n" +
                    "    }\n" +
                    "\n" +
                    "    /*无缓存有验证获取" + introspectedTable.getRemarks() + "实例列表*/\n" +
                    "    public ListResponse listWithoutCache(String token, Integer page, Integer limit, String orderBy){\n" +
                    "        //拿到获取的实例\n" +
                    "        ListResponse listResponse = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "Client.listWithoutCache(token, page, limit, orderBy);\n" +
                    "        //如果成功\n" +
                    "        if(listResponse.getStatus()==STATUS_SUCCESS){\n" +
                    "            //返回结果\n" +
                    "            return listResponse;\n" +
                    "        }\n" +
                    "        //抛出异常\n" +
                    "        throw new CurrentServerException(listResponse.getStatus(),listResponse.getMessage());\n" +
                    "    }\n" +
                    "\n" +
                    "    /*只读缓存有验证获取" + introspectedTable.getRemarks() + "实例列表*/\n" +
                    "    public ListResponse listWithMock(String token, Integer page, Integer limit, String orderBy){\n" +
                    "        //拿到获取的实例\n" +
                    "        ListResponse listResponse = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "Client.listWithMock(token, page, limit, orderBy);\n" +
                    "        //如果成功\n" +
                    "        if(listResponse.getStatus()==STATUS_SUCCESS){\n" +
                    "            //返回结果\n" +
                    "            return listResponse;\n" +
                    "        }\n" +
                    "        //抛出异常\n" +
                    "        throw new CurrentServerException(listResponse.getStatus(),listResponse.getMessage());\n" +
                    "    }\n" +
                    "\n" +
                    "    /*按条件查询" + introspectedTable.getRemarks() + "实例列表*/\n" +
                    "    public ListResponse query(String token, Integer page, Integer limit, String orderBy, \n" + MybatisUtilCommon.getParameterWithoutAnnotationForColumnsWithMaxMinPrefixSuffixLike(introspectedTable) + "){\n" +
                    "        //拿到获取的实例\n" +
                    "        ListResponse listResponse = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "Client.query(token, page, limit, orderBy, \n" + MybatisUtilCommon.getParameterWithoutAnnotationWithoutTypeForColumnsWithMaxMinPrefixSuffixLike(introspectedTable) + ");\n" +
                    "        //如果成功\n" +
                    "        if(listResponse.getStatus()==STATUS_SUCCESS){\n" +
                    "            //返回结果\n" +
                    "            return listResponse;\n" +
                    "        }\n" +
                    "        //抛出异常\n" +
                    "        throw new CurrentServerException(listResponse.getStatus(),listResponse.getMessage());\n" +
                    "    }\n" +
                    "\n" +
                    "    /*获取" + introspectedTable.getRemarks() + "的不同字段的不重复值列表*/\n" +
                    "    public SimpleResponse distinct(String token, String field){\n" +
                    "        //拿到获取的实例\n" +
                    "        SimpleResponse simpleResponse = " + MybatisUtilCommon.toLowerCaseAtFirstCharacter(tableName) + "Client.distinct(token, field);\n" +
                    "        if(simpleResponse.getStatus()==STATUS_SUCCESS){\n" +
                    "            //返回结果\n" +
                    "            return simpleResponse;\n" +
                    "        }\n" +
                    "        //抛出异常\n" +
                    "        throw new CurrentServerException(simpleResponse.getStatus(),simpleResponse.getMessage());\n" +
                    "    }\n" +
                    "\n" +
                    "\n}" +
                    "\n"
            );
            System.out.println("write to file success : " + file.getPath());
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            MybatisUtilCommon.closeBufferWriterAndFileOutputStream(fileOutputStream, bufferedWriter);
        }
    }

}
